﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;

namespace WeChartWorkRobot.Common
{
    public class XmlHelper
    {
        public static void XmlSerializeToFile(object o, string path)
        {
            XmlHelper.XmlSerializeToFile(o, path, Encoding.UTF8);
        }

        public static void XmlSerializeToFile(object o, string path, Encoding encoding)
        {
            bool flag = string.IsNullOrEmpty(path);
            if (flag)
            {
                throw new ArgumentNullException("path");
            }
            using (FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write))
            {
                XmlHelper.XmlSerializeInternal(fileStream, o, encoding);
            }
        }

        private static void XmlSerializeInternal(Stream stream, object o, Encoding encoding)
        {
            bool flag = o == null;
            if (flag)
            {
                throw new ArgumentNullException("o");
            }
            bool flag2 = encoding == null;
            if (flag2)
            {
                throw new ArgumentNullException("encoding");
            }
            XmlSerializer xmlSerializer = new XmlSerializer(o.GetType());
            using (XmlWriter xmlWriter = XmlWriter.Create(stream, new XmlWriterSettings
            {
                Indent = true,
                NewLineChars = "\r\n",
                Encoding = encoding,
                IndentChars = "    "
            }))
            {
                xmlSerializer.Serialize(xmlWriter, o);
            }
        }

        public static string XmlSerializerObject(object o)
        {
            bool flag = o == null;
            if (flag)
            {
                throw new ArgumentNullException("o");
            }
            Encoding uTF = Encoding.UTF8;
            XmlSerializer xmlSerializer = new XmlSerializer(o.GetType());
            string result;
            using (MemoryStream memoryStream = new MemoryStream())
            {
                XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
                xmlWriterSettings.Indent = true;
                xmlWriterSettings.NewLineChars = "\r\n";
                xmlWriterSettings.Encoding = uTF;
                xmlWriterSettings.OmitXmlDeclaration = true;
                xmlWriterSettings.IndentChars = "    ";
                XmlSerializerNamespaces xmlSerializerNamespaces = new XmlSerializerNamespaces();
                xmlSerializerNamespaces.Add("", "");
                using (XmlWriter xmlWriter = XmlWriter.Create(memoryStream, xmlWriterSettings))
                {
                    xmlSerializer.Serialize(xmlWriter, o, xmlSerializerNamespaces);
                    xmlWriter.Close();
                }
                memoryStream.Position = 0L;
                using (StreamReader streamReader = new StreamReader(memoryStream, uTF))
                {
                    result = streamReader.ReadToEnd();
                }
            }
            return result;
        }
        public static string XmlSerialize(object o, Encoding encoding)
        {
            string result;
            using (MemoryStream memoryStream = new MemoryStream())
            {
                XmlHelper.XmlSerializeInternal(memoryStream, o, encoding);
                memoryStream.Position = 0L;
                using (StreamReader streamReader = new StreamReader(memoryStream, encoding))
                {
                    result = streamReader.ReadToEnd();
                }
            }
            return result;
        }

        public static object XmlDeserialize(Stream stream, Type destType)
        {
            bool flag = stream == null;
            if (flag)
            {
                throw new ArgumentNullException("stream");
            }
            bool flag2 = destType == null;
            if (flag2)
            {
                throw new ArgumentNullException("destType");
            }
            XmlSerializer xmlSerializer = new XmlSerializer(destType);
            object result;
            using (StreamReader streamReader = new StreamReader(stream))
            {
                result = xmlSerializer.Deserialize(streamReader);
            }
            return result;
        }

        public static object XmlDeserialize(string s, Type destType, Encoding encoding)
        {
            bool flag = string.IsNullOrEmpty(s);
            if (flag)
            {
                throw new ArgumentNullException("s");
            }
            bool flag2 = destType == null;
            if (flag2)
            {
                throw new ArgumentNullException("destType");
            }
            bool flag3 = encoding == null;
            if (flag3)
            {
                throw new ArgumentNullException("encoding");
            }
            object result;
            using (MemoryStream memoryStream = new MemoryStream(encoding.GetBytes(s)))
            {
                result = XmlHelper.XmlDeserialize(memoryStream, destType);
            }
            return result;
        }

        public static T XmlDeserialize<T>(string s)
        {
            return (T)((object)XmlHelper.XmlDeserialize(s, typeof(T), Encoding.UTF8));
        }

        public static T XmlDeserializeFromFile<T>(string path)
        {
            bool flag = string.IsNullOrEmpty(path);
            if (flag)
            {
                throw new ArgumentNullException("path");
            }
            T result;
            try
            {
                using (FileStream fileStream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    result = (T)((object)XmlHelper.XmlDeserialize(fileStream, typeof(T)));
                }
            }
            catch (Exception innerException)
            {
                throw new InvalidDataException("XML反序列失败，当前文件：" + path, innerException);
            }
            return result;
        }
    }
}
